package com.zy.service;

import com.zy.dao.HomeMapper;
import com.zy.entity.Page;
import com.zy.entity.Video;
import com.zy.entity.VideoText;
import com.zy.entity.VideoType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 张宇
 * @date 2021/7/30 19:16
 */
@Slf4j
@Service
public class HomeService
{

    @Autowired
    private HomeMapper homeMapper;

    @Value("${shangchuanlujing}")
    private String shangchuanlujing;//上传文件的路径

    @Value("${fanhuilujing}")
    private String fanhuilujing;//返回的路径

    /**
     * 通过parentId查询集合
     * @param parentId
     * @return
     */
    public List<VideoType> findVideoListByParentId(int parentId)
    {
        return homeMapper.findVideoListByParentId(parentId);
    }

    /**
     * 上传文件
     * @param file
     */
    public String upFile(MultipartFile file)
    {
        String filepath = null;//最终存入数据库的图片地址
        if (!file.isEmpty())
        {
            try
            {
                // 时间戳
                String[] st = file.getOriginalFilename().split("\\.");
                int suffixIndex = st.length - 1;
                String exe = st[suffixIndex];
                Long timeStamp = System.currentTimeMillis();

                String fileName = timeStamp + "." + exe;

                String filePath = shangchuanlujing + fileName;
                filepath = fanhuilujing + fileName;

                log.info("上传路径:{}",filePath);
                log.info("返回路径:{}",filepath);

                File saveFile = new File("/test");
                if (!saveFile.exists())
                {
                    saveFile.mkdirs();
                }
                File uploadFile = new File(filePath);
                file.transferTo(uploadFile);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return filepath;
        }
        else
        {
            return "上传失败!";
        }
    }

    /**
     * 增加视频信息
     * @param video
     */
    public void addVideo(Video video)
    {
        homeMapper.addVideo(video);
    }

    /**
     * 最新视频
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<VideoText> videoListByPageNoAndPageSize(int pageNo, int pageSize)
    {
        Page<VideoText> page = new Page<>();

        page.setPageNo(pageNo);//设置当前页码

        page.setPageSize(pageSize);//设置每页数量

        page.setPagesTotalCount(homeMapper.videoNum());//设置总记录数

        int pageTotal = page.getPagesTotalCount()/pageSize;

        if(page.getPagesTotalCount()%pageSize>0)
        {
            pageTotal++;
        }

        page.setPageTotal(pageTotal);//设置页码总数

        int begin = (pageNo-1)*pageSize;//起始数

        page.setItems(homeMapper.videoListByBeginAndPageSize(begin,pageSize,"3"));//设置当前页数据

        return page;
    }

    /**
     * 热门视频
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<VideoText> hotVideoListByPageNoAndPageSize(int pageNo, int pageSize)
    {
        Page<VideoText> page = new Page<>();

        page.setPageNo(pageNo);//设置当前页码

        page.setPageSize(pageSize);//设置每页数量

        page.setPagesTotalCount(homeMapper.videoNum());//设置总记录数

        int pageTotal = page.getPagesTotalCount()/pageSize;

        if(page.getPagesTotalCount()%pageSize>0)
        {
            pageTotal++;
        }

        page.setPageTotal(pageTotal);//设置页码总数

        int begin = (pageNo-1)*pageSize;//起始数

        page.setItems(homeMapper.hotVideoListByBeginAndPageSize(begin,pageSize,"3"));//设置当前页数据

        return page;
    }

    /**
     * 通过id查找视频信息
     * @param id
     * @return
     */
    public VideoText findVideoById(int id)
    {
        return homeMapper.findVideoById(id);
    }

    /**
     * 通过id修改浏览量
     * @param id
     * @param videoView
     */
    public void addVideoViewById(Integer id, int videoView)
    {
        homeMapper.addVideoViewById(id, videoView);
    }

    /**
     * 得到自己的视频列表
     * @param userId
     * @return
     */
    public Map<String,Object> getOneselfVideoList(int userId, int pageNo, int pageSize) {
        List<VideoText> list = homeMapper.getOneselfVideoList(userId);
        List<VideoText> pageList = list.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());

        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("rows",pageList);
        map.put("total",list.size());//总条数
        map.put("pageTotal",list.size()%pageSize == 0 ? list.size()/pageSize : list.size()/pageSize + 1);//页码数

        return map;
    }

}
